use std::{cmp::Ordering, io};

use rand::Rng;

fn main() {
    println!("猜数!!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    println!("神秘数字是: {}", secret_number);
    loop {
        println!("请猜一个数!");

        let mut guess = String::new();

        io::stdin().read_line(&mut guess).expect("无法读取!");

        let guess: u32 = match guess.trim().parse(){
            Ok(guess)=>guess,
            Err(_)=>{
                print!("invalid input!");
                continue;
            }
        };

        println!("你猜测的数是:{}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("too small"),
            Ordering::Greater => println!("too big"),
            Ordering::Equal => {
                println!("bingo");
                break;
            }
        }
    }
}
